#!/usr/bin/env python3

import sys
from pathlib import Path
import os
import subprocess


def create_symlink(targets, dest_dir='.'):
    """
    Create symbolic links for the given targets in the specified destination directory.

    :param targets: List of file paths to create symbolic links for.
    :param dest_dir: Directory where the symbolic links will be created.
    """
    dest_path = Path(dest_dir).resolve()
    retval = None

    for target in targets:
        target_path = Path(target)
        resolved_target_path = Path(target).resolve()
        symlink_path = dest_path / target_path.name

        # Calculate relative path from the destination directory to the target
        # relative_target_path = target_path.relative_to(dest_path)
        relative_target_path = os.path.relpath(resolved_target_path, symlink_path.parent)

        while True:
            try:
                symlink_path.symlink_to(relative_target_path)
                print(f'Created symlink: {symlink_path} -> {relative_target_path}')
                break
            except FileExistsError:
                symlink_path = Path(f'{symlink_path}_')
                continue
            except OSError as e:
                print(f'Failed to create symlink for {target}: {e}')
                break
        retval = symlink_path.relative_to(dest_path)
    return retval


if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('Usage: python script.py <lf-process-ld> <target1> <target2> ...')
        sys.exit(1)

    id = sys.argv[1]
    targets = sys.argv[2:]
    retval = create_symlink(targets)
    subprocess.run(['lf', '-remote', f'send {id} select {retval}'])
